package yxy.cherry.paycenter.service;

import javax.servlet.http.HttpServlet;

import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import yxy.apple.logger.Logger;
import yxy.cherry.paycenter.Configuration;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinAndroid;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinPrePayIos;

public class SdkHttpsService {
	// http 服务器
	private Server httpServer;
	private static SdkHttpsService instance;
	private ServletContextHandler context;
	private int port;

	public static SdkHttpsService getInstance() {
		if (instance == null) {
			instance = new SdkHttpsService();
		}
		return instance;
	}	
	
	static final Logger logger = Logger.create(SdkHttpService.class);
	
	public void httpsinit(Configuration configuration) {
		httpServer = new Server();
		HttpConfiguration https_config = new HttpConfiguration();
		https_config.setSecureScheme("https");

		final SslContextFactory factory = new SslContextFactory();

//		factory.setKeyStorePath("./ssl.jks");
//		factory.setKeyStorePassword("yixinyou");
		factory.setKeyStorePath(configuration.SSL.JKSFilename);
		factory.setKeyStorePassword(configuration.SSL.JKSPassword);
			
		final ServerConnector connector = new ServerConnector(httpServer, factory,
				new HttpConnectionFactory(https_config));
		connector.setPort(configuration.HttpsProt);
		httpServer.addConnector(connector);
		
		// ----------------------
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		httpServer.setHandler(context);
		initServlet(context);
		
		try {
			System.out.println("*********sdk service https begin:" + configuration.HttpsProt);
			httpServer.start();
			System.out.println("*********sdk service https end:" + configuration.HttpsProt);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initServlet(ServletContextHandler context) {
		//ios 创建订单
		Servlet_WeiXinPrePayIos weixin_iosprepay = new Servlet_WeiXinPrePayIos();
		context.addServlet(new ServletHolder(weixin_iosprepay), weixin_iosprepay.getUrl());
		logger.info("weixin_iosprepay :" + weixin_iosprepay.getUrl());
		
		Servlet_WeiXinAndroid weixin_android = new Servlet_WeiXinAndroid();
		context.addServlet(new ServletHolder(weixin_android), weixin_android.getUrl());
		logger.info("weixin_android :" + weixin_android.getUrl());
	}

	public void addServlet(String path, HttpServlet servlet) {
		context.addServlet(new ServletHolder(servlet), path);
	}

	public void start() {
		try {
			httpServer.start();
			System.out.println("*********gm service start:" + port);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stop() {
		if (httpServer != null) {
			try {
				httpServer.stop();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
